package com.sdx.console.transaction.service;

import java.util.Date;
import java.util.List;

import com.ibeetl.admin.core.entity.BaseEntity;
import com.sdx.console.transaction.dao.FinTransactionDao;
import com.sdx.console.transaction.entity.FinTransaction;
import com.sdx.console.transaction.entity.RefundExport;
import com.sdx.console.transaction.web.query.FinTransactionRefundQuery;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ibeetl.admin.core.util.PlatformException;

import com.sdx.console.transaction.dao.FinTransactionRefundDao;
import com.sdx.console.transaction.entity.FinTransactionRefund;
import com.ibeetl.admin.core.service.BaseService;

import javax.annotation.Resource;

/**
 * 退款 Service
 * @author tanbc
 */

@Service
@Transactional
public class FinTransactionRefundService extends BaseService<FinTransactionRefund>{

    @Resource
    private FinTransactionRefundDao refundDao;

	@Resource
	private FinTransactionDao finTransactionDao;

    public PageQuery<FinTransactionRefund>queryByCondition(PageQuery query){
        PageQuery ret =  refundDao.queryByCondition(query);
        queryListAfter(ret.getList());
        return ret;
    }

    public void batchDelFinTransactionRefund(List<String> ids){
        try {
            refundDao.batchDelFinTransactionRefundByIds(ids);
        } catch (Exception e) {
            throw new PlatformException("批量删除退款失败", e);
        }
    }

    public String selectMchId(String refundId) {
    	return refundDao.selectMchId(refundId);
    }

	/**
	 * 判断，如果订单退款总金额已经达到订单总金额，则更改订单状态为已退款
	 * @param refundId
	 */
	public void transRefund(String refundId) {
		//查询还有多少钱未退款
		FinTransaction transaction= refundDao.selectRemainMoney(refundId);
		//更新为已退款
		if (transaction.getTotalMoney()<1) {
			FinTransaction template = new FinTransaction();
			template.setTransactionId(transaction.getTransactionId());
			template.setPayFlag(4);
			finTransactionDao.updateTemplateById(template);
		}
	}

	public BaseEntity selectAudit(String refundId){
	    return refundDao.selectAudit(refundId);
    }

    public List<BaseEntity>  selectOrderType(){
	    return refundDao.selectOrderType();
    }

    public List<BaseEntity> selectImgsById(String refundId){
	    return refundDao.selectImgsById(refundId);
    }

    public List<RefundExport> queryByConditionExport(FinTransactionRefundQuery condtion) {
	    return refundDao.queryByConditionExport( condtion);
    }

    public List<FinTransactionRefund> findRefundRecord(FinTransactionRefund refund) {
        return refundDao.findRefundRecord( refund);
    }

    public List<FinTransaction> findTranRecord(FinTransactionRefund refund) {
        return refundDao.findTranRecord( refund);
    }

    public void batchUpdateFinTransactionRefund(List<String> idList, String name, Long id, Date date) {
        refundDao.batchUpdateFinTransactionRefund(idList,name,id,date);
    }

    public void updateFinTrans(List<String> ids) {
        refundDao.updateFinTrans(ids);
    }
}
